package org.csu.measure.controller;

import org.csu.measure.service.GetData;
import org.csu.measure.domain.Class;
import org.csu.measure.domain.LKMetrics;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/toLK")
@SessionAttributes(value = {"toLK"})
public class LKController {

    //跳转到LK界面
    @GetMapping("/goToLKMetrics")
    public String signonForm(Model model){
        return "LKMetric";
    }

    @PostMapping("/getClassList")
    public String getClassList(Model model, HttpSession session, @RequestParam("code")String code) throws IOException {

        String dir = "test.xml";
        File file = new File(dir);
//如果文件不存在，创建文件
        if (!file.exists())
            file.createNewFile();
//创建FileWriter对象
        FileWriter writer = new FileWriter(file);
//向文件中写入内容
        writer.write(code);
        writer.flush();
        writer.close();

        GetData data = new GetData();
        List<Class> classList = data.getData();

        ArrayList<LKMetrics> lkList = new ArrayList<>();
        for(int i = 0; i < classList.size(); i++){
             lkList.add(classList.get(i).getLk());
        }

        model.addAttribute("classList", classList);
        session.setAttribute("lkList",lkList);
        model.addAttribute("code", code);
        return "LKMetric";
    }

    @PostMapping("/setLine")
    @ResponseBody
    public ArrayList<LKMetrics> setLine(HttpSession session) {
        ArrayList<LKMetrics> lkList = (ArrayList<LKMetrics>)session.getAttribute("lkList");
        return lkList;
    }

}
